﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using Volo.Abp;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using FourGroups.Project;
using _8Group.MES.IAppService.Clients.Dto;
using _8Group.MES.IAppService.Clients;


namespace _8Group.MES.ClientManagement
{
    [ApiExplorerSettings(GroupName = "周扬")]
    /// <summary>
    /// 客户管理相关实现
    /// </summary>
    public class ClientAppServic : MESAppService, IClientAppService
    {
        /// <summary>
        /// 客户仓储
        /// </summary>
        private readonly IRepository<MdClient, int> _clientRepository;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="clientRepository">客户仓储</param>
        public ClientAppServic(IRepository<MdClient, int> clientRepository)
        {
            _clientRepository = clientRepository;
        }

        /// <summary>
        /// 添加客户
        /// </summary>
        /// <param name="input">客户信息</param>
        /// <returns></returns>
        public async Task<int> AddClient(CreateClienDto input)
        {
            var client = AutoMapperExt.MapTo<MdClient>(input);
            client.CreateBy = "admin";
            client.CreateTime = DateTime.Now;
            await _clientRepository.InsertAsync(client);
            return 1;
        }

        /// <summary>
        /// 修改客户
        /// </summary>
        /// <param name="input">客户信息</param>
        /// <returns></returns>
        public async Task<bool> UpdateClient(UpdateClienDto input)
        {
            var client = AutoMapperExt.MapTo<MdClient>(input);
            client.UpdateBy = "admin";
            client.UpdateTime = DateTime.Now;
            await _clientRepository.UpdateAsync(client);
            return true;
        }

        /// <summary>
        /// 删除客户
        /// </summary>
        /// <param name="id">客户编号</param>
        /// <returns></returns>
        public async Task<bool> DeleteClient(int id)
        {
            var client = await _clientRepository.GetAsync(id);
            await _clientRepository.DeleteAsync(client, true);
            return true;
        }

        /// <summary>
        /// 显示客户信息
        /// </summary>
        /// <returns></returns>
        public async Task<string> GetMdClients(string? Code, string? Name, string? Nick, int pageIndex = 1, int pageSize = 10)
        {
            var mdClients = await _clientRepository.GetQueryableAsync();

            mdClients = mdClients.WhereIf(!string.IsNullOrWhiteSpace(Code), x => x.ClientCode.Contains(Code));
            mdClients = mdClients.WhereIf(!string.IsNullOrWhiteSpace(Name), x => x.ClientName.Contains(Name));
            mdClients = mdClients.WhereIf(!string.IsNullOrWhiteSpace(Nick), x => x.ClientNick.Contains(Nick));

            int totalCount = mdClients.Count();
            mdClients = mdClients.OrderByDescending(x => x.CreateTime).Skip((pageIndex - 1) * pageSize).Take(pageSize);

            var result = new ResponseModel
            {
                data = mdClients.ToList(),
                code = "0",
                count = totalCount,
                msg = "请求成功"
            };

            return JsonConvert.SerializeObject(result, Formatting.Indented);
        }
    }
}
